<?php
/**
 *
 *
 *
 */
class Yz_Form_Element_UploadImage
    extends Yz_Form_Element_ForeignKey
{

    protected $name;
    protected $value = 0;
    protected $fileName = "";

    /** the model which will handle the upload;
     *      can be set using modelName config, or determined as 'Upload'+this.name
     *  @var Yz_Model_Interface
     */
    protected $model;


    /**
     *
     */
    public function render(Yz_Template_Interface $template)
    {
        $action       = $template->YzAdmin ?: $template->webPrefix;
        $action      .= $this->getUploadAction();
        $curValOutput = $this->renderCurrentValue($template);
        $elName       = $this->name;
        $buttonTitle  = $template->translate($elName.':Upload');
        $buttonUpload = $template->translate('Upload');
        $buttonCancel = $template->translate('Cancel');
        $currentId = $this->value ? $this->value->id : 0;
        $js = <<<js
function initUpload(a_clicked, name) {
    Yz.loadClass('Yz.Upload', function() {
        // is the ref to Yz.Upload class (not instance)
        this.createUpload(name)
            .setUploadAction(a_clicked.getAttribute('href'))
            .showDialog({
                title:  a_clicked.getAttribute('title')
            })
    })
}
js;
        $template->includeJS($this->_unclass_(), $js);
        $o = <<<html
            <div id="yz-upload-{$elName}">
                <input type="hidden" name="{$elName}" value="{$currentId}" />
                <div class="yz-upload-value">{$curValOutput}</div>
                <a href="{$action}" title="{$buttonTitle}"
                    onclick="initUpload(this, '{$elName}'); return false">{$buttonUpload}</a>
            </div>\n
html;
        return $o;
    }


    /** get the module - owner of the upload model
     *      try config 'moduleName' or use the form's owner module
     *  @return Yz_Module_Interface
     */
    protected function getModule()
    {
        $module =& $this->module;
        if (! $module) {
            $module = $this->getModel()->getModule();
        }
        return $module;
    }

    /** get upload model: try config 'modelName'
     *      or derive the modelName as 'Upload'+this.name
     *  @return Yz_Model_Interface
     */
    protected function getModel()
    {
        $model =& $this->model;
        if (! $model)
            $model = $this->field->getModel();
        return $model;
    }


    protected function getUploadAction()
    {
        $a = Yz_String::fromCamelCase(
            $this->getModule()->getName()
            . '/'
            . Yz_String::fromCamelCase($this->getModel()->getUploadController())
        );
        return $a;
    }

    /**
     *
     */
    protected function getUploadPath()
    {
        $path = $this->getModel()->getUploadPath();
        return $path;
    }

    /**
     *
     */
    protected function renderCurrentValue(Yz_Template_Interface $template)
    {
        $o = "";
        $webRoot = $template->webRoot;
        if ($this->value) {
            $uploadPath = $this->value->getUploadPath();
            $fileName = $this->value->filename;
            $w = $this->value->width;
            $h = $this->value->height;
            $o .= <<<html
                <a href="{$webRoot}{$uploadPath}{$fileName}"><img
                    src="{$webRoot}{$uploadPath}{$fileName}" alt="Upload #{$this->value->id} ({$w}x{$h})" /></a>
html;
        }
        return $o;
    }

}
